<?php
$sMsg="";
$Action="?mod=adv.add&method=add";
$Submit="Đồng ý";
$TitleTable="Tạo mới";
if(isset($_GET['pid'])){ $pid = $_GET['pid'];}else{ $pid=0;}
if ($Method=="add")
{
	if(isset($_POST['submit'])){
		$qc_tieude = addslashes($_POST['qc_tieude']);
		//$qc_id_chungloai = $_POST['qc_id_chungloai'];
		//$qc_kieuchungloai = $_POST['qc_kieuchungloai'];
		
		if (isset($_POST['qc_src']))
		{
		   $dir="upload/";
		   $file= $dir. basename($_FILES['fileuploaded']['name']);
		   if(move_uploaded_file($_FILES['fileuploaded']['tmp_name'], $file)) {
		    echo "The file ".  basename( $_FILES['fileuploaded']['name']). " has been uploaded";
			   $qc_src = $file;
			} else{
		    echo "There was an error uploading the file, please try again!";
			}
		}			
		
		//$qc_src = $_POST['qc_src'];
		$qc_width = $_POST['qc_width'];
		$qc_height = $_POST['qc_height'];
		$qc_kieu_src = $_POST['qc_kieu_src'];
		//$qc_vitri = $_POST['qc_vitri'];
		$qc_lienket = $_POST['qc_lienket'];
		$qc_chitiet = addslashes($_POST['qc_chitiet']);
		$qc_thutu = $_POST['qc_thutu'];
		
		$sql = "INSERT INTO `vstar_quangcao` (`qc_tieude`, `qc_src`, `qc_width`, `qc_height`, `qc_kieu_src`, `qc_lienket`, `qc_chitiet`, `qc_thutu`, `qc_ngaythang`, `qc_hienthi`)";
		$sql .= " VALUES('".$qc_tieude."','".$qc_src."','".$qc_width."','".$qc_height."','".$qc_kieu_src."','".$qc_lienket."','".$qc_chitiet."','".$qc_thutu."',now(),1)";
		//echo $sql;
		mysql_query($sql);
		Redirect("Đã thêm mới thành công","?mod=adv.add");
	}
}
if($Method=="edit")
{
	$id=$_GET["id"];
	$sql="select * from ".$prefix."quangcao where qc_id=$id";
	$result=mysql_query($sql);
	if(mysql_num_rows($result))
	{
		$row=mysql_fetch_array($result);
					$qc_id = $row[qc_id];
					//$qc_id_chungloai = $row[qc_id_chungloai];
					//$qc_kieuchungloai = $row[qc_kieuchungloai];
					$qc_tieude = $row[qc_tieude];
					$qc_src = $row[qc_src];
					$qc_width = $row[qc_width];
					$qc_height = $row[qc_height];
					$qc_kieu_src = $row[qc_kieu_src];
					//$qc_vitri = $row[qc_vitri];
					$qc_ngaythang = format_date($row[qc_ngaythang]);
					$qc_hienthi = $row[qc_hienthi];
					$qc_lienket = $row[qc_lienket];
					$qc_chitiet = $row[qc_chitiet];
		
		$Submit="Cập nhập";
		$TitleTable="Sửa thông tin";
		$Action="?mod=adv.add&method=update&id=$id";
	}
}

if ($Method=="update")
{
	if(isset($_POST['submit'])){
		$id=$_GET["id"];
		$qc_tieude = addslashes($_POST['qc_tieude']);
		//$qc_id_chungloai = $_POST['qc_id_chungloai'];
		//$qc_kieuchungloai = $_POST['qc_kieuchungloai'];
		
			$dir="../upload/";
		   $file= $dir. basename($_FILES['fileuploaded']['name']);
		   if(move_uploaded_file($_FILES['fileuploaded']['tmp_name'], $file)) {
		    echo "The file ".  basename( $_FILES['fileuploaded']['name']). " has been uploaded";
			   $qc_src = basename( $_FILES['fileuploaded']['name']);
			   echo " ".$qc_src;
			} else{
		    echo "There was an error uploading the file, please try again!";
			echo $file;
			}
		//$qc_src = $_POST['qc_src'];
		$qc_width = $_POST['qc_width'];
		$qc_height = $_POST['qc_height'];
		$qc_kieu_src = $_POST['qc_kieu_src'];
		//$qc_vitri = $_POST['qc_vitri'];
		$qc_lienket = $_POST['qc_lienket'];
		$qc_chitiet = addslashes($_POST['qc_chitiet']);
		$qc_thutu = $_POST['qc_thutu'];
		
		$sql = "UPDATE `vstar_quangcao` SET `qc_tieude` = '".$qc_tieude."', `qc_src` = '".$qc_src."', `qc_width` = '".$qc_width."', `qc_height` = '".$qc_height."', `qc_kieu_src` = '".$qc_kieu_src."', `qc_lienket` = '".$qc_lienket."', `qc_chitiet` = '".$qc_chitiet."', `qc_thutu` = '".$qc_thutu."' WHERE qc_id = '".$id."'";
		//echo $sql;
		mysql_query($sql);
		Redirect("Cập nhập thành công","?mod=adv");
	}
}


/*
if(isset($_GET['KCL'])){
	$KCL = intval($_GET['KCL']);
}else{
	if($method == 'update'){
		$KCL = $qc_kieuchungloai;
	}else{
		$KCL = 0;
	}
}
 */
?>
<script>/*	
	function funKieuChungLoai(KCL){
		window.location = '?mod=adv.add&KCL=' + KCL;
	}*/
</script>

<!--Bat dau form tao sua -->
<form name="frmHuyen" id="frmHuyen" action="<?=$Action;?>" method="post" enctype="multipart/form-data">
	<table width="100%" border="1" style="border-collapse:collapse " bordercolor="#B2DAEE">
	  <tr class="newsList-content-head">
		<td height="28" colspan="2" align="center"><?=$TitleTable?></td>
	  </tr>		  
	  <tr>
	      <td width="254" align="right"><strong>
		  Tiêu đề :</strong></td>
	      <td width="497"><input name="qc_tieude" type="text" id="qc_tieude" value="<?=$qc_tieude?>" size="50" /></td>
	  </tr>
	  <!--
	  <tr>
		<td width="254" align="right"><strong>Kiểu chủng loại :</strong></td>
		<td width="497">
		<select name="qc_kieuchungloai" id="qc_kieuchungloai" onchange="funKieuChungLoai(this.value);">
			<option value="0" <? if( ($qc_id_chungloai == 0) || ($KCL == 0) ){ ?> selected="selected" <? } ?>>Sản phẩm</option>
			<option value="1" <? if( ($qc_id_chungloai == 1) || ($KCL == 1) ){ ?> selected="selected" <? } ?>>Rao vặt</option>
			<option value="2" <? if( ($qc_id_chungloai == 2) || ($KCL == 2) ){ ?> selected="selected" <? } ?>>Tuyển dụng</option>
		  </select>
		</td>
	  </tr>

	  <tr>
		<td width="254" align="right"><strong>Chủng loại :</strong></td>
		<td width="497">
			<select name="qc_id_chungloai" id="qc_id_chungloai"> 
	  	<?
			if($KCL == 0){
				$rs = $db->query("SELECT * FROM ".$prefix."loaisanpham WHERE c_hienthi = 1 AND c_id_cha = 0 ORDER BY c_thutu ASC");
			}else if($KCL == 1){
				$rs = $db->query("SELECT * FROM ".$prefix."loairaovat WHERE rvc_hienthi = 1 AND rvc_id_cha = 0 ORDER BY rvc_thutu ASC");
			}else{
				$rs = $db->query("SELECT * FROM ".$prefix."loaituyendung WHERE ltd_hienthi = 1 AND ltd_id_cha = 0 ORDER BY ltd_thutu ASC");
			}
					while($row = $db->fetch_array($rs)){
					if($KCL == 0){
						$c_ten = $myString->unQuote(stripslashes($row[c_ten]));
						$c_id = $row[c_id];
					}else if($KCL == 1){
						$c_ten = $myString->unQuote(stripslashes($row[rvc_ten]));
						$c_id = $row[rvc_id];
					}else{
						$c_ten = $myString->unQuote(stripslashes($row[ltd_ten]));
						$c_id = $row[ltd_id];
					}
	?>	
                    <optgroup label="<?=$c_ten?>">
	<?
					if($KCL == 0){
						$rs2 = $db->query("SELECT * FROM ".$prefix."loaisanpham WHERE c_hienthi = 1 AND c_id_cha = '".$c_id."' ORDER BY c_id DESC");
					}else if($KCL == 1){
						$rs2 = $db->query("SELECT * FROM ".$prefix."loairaovat WHERE rvc_hienthi = 1 AND rvc_id_cha = '".$c_id."' ORDER BY rvc_id DESC");
					}else{
						$rs2 = $db->query("SELECT * FROM ".$prefix."loaituyendung WHERE ltd_hienthi = 1 AND ltd_id_cha = '".$c_id."' ORDER BY ltd_id DESC");
					}
						while($row2 = $db->fetch_array($rs2)){
						if($KCL == 0){
							$c_ten2 = $myString->unQuote(stripslashes($row2[c_ten]));
							$c_id2 = $row2[c_id];
						}else if($KCL == 1){
							$c_ten2 = $myString->unQuote(stripslashes($row2[rvc_ten]));
							$c_id2 = $row2[rvc_id];
						}else{
							$c_ten2 = $myString->unQuote(stripslashes($row2[ltd_ten]));
							$c_id2 = $row2[ltd_id];
						}
	?>
				<option value="<?=$c_id2?>" <? if($qc_id_chungloai == $c_id2){ ?> selected="selected" <? } ?>><?=$c_ten2?></option>
			<? } ?>
					</optgroup>
				<? } ?>
		  </select>
		</td>
	  </tr>	
	  -->  
	  <tr>
		<td width="254" align="right"><strong>Kiểu Topic :</strong></td>
		<td width="497">
			<input name="qc_kieu_src" type="radio" value="0" <? if($qc_kieu_src == 0){ ?> checked="checked" <? } ?>/>
		  Image 
	      <input name="qc_kieu_src" type="radio" value="1"  <? if($qc_kieu_src == 1){ ?> checked="checked" <? } ?>/> 
	      Flash </td>
	  </tr>
	  <tr>
	    <td width="254" align="right"><strong>Đường dẫn file : </strong></td>
	    <td><input name="fileuploaded" type="file" id="qc_src" size="50"/></td>
      </tr>
	  <tr>
	    <td width="254" align="right"><strong>Width : </strong></td>
	    <td><input name="qc_width" type="text" id="qc_width" size="10" value="<?=$qc_width?>"/> Pixel</td>
      </tr>
	  <tr>
	    <td width="254" align="right"><strong>Height : </strong></td>
	    <td><input name="qc_height" type="text" id="qc_height" size="10" value="<?=$qc_height?>" /> Pixel</td>
      </tr>
      <!--
	  <tr>
	    <td width="254" align="right"><strong>Vị trí : </strong></td>
	    <td><select name="qc_vitri" id="qc_vitri">
			<option value="0" <? if($qc_vitri == 0){ ?> selected="selected" <? } ?>>Bên phải</option>
	      </select>
	    </td>
      </tr>
     -->
	  <tr>
	    <td width="254" align="right"><strong>Liên kết : </strong></td>
	    <td><input name="qc_lienket" type="text" id="qc_lienket" size="50" value="<?=$qc_lienket?>"/></td>
      </tr>
	  <tr>
	    <td width="254" align="right"><strong>Chi tiết : </strong></td>
	    <td><textarea name="qc_chitiet" cols="50" rows="4" id="qc_chitiet"><?=$qc_chitiet?></textarea></td>
      </tr>
	  <tr>
	    <td width="254" align="right"><strong>Thứ tự : </strong></td>
	    <td><select name="qc_thutu" id="qc_thutu">
		<?
			$rs = $db->query("SELECT MAX(qc_thutu) AS max_thutu FROM ".$prefix."quangcao");
			$row = $db->fetch_array($rs);
			$thutu = $row[max_thutu];
			if($Method == 'update'){
				for($i=1;$i<$thutu;$i++){
		?>
					<option value="<?=$i?>" <? if($qc_thutu == $i){ ?> selected="selected" <? } ?>><?=$i?></option>
		<?
				}
			}else{
				for($i = $thutu+1; $i>= 1; $i--){
		?>
					<option value="<?=$i?>" <? if($qc_thutu == $i){ ?> selected="selected" <? } ?>><?=$i?></option>
		<? 		} 
		
			}?>
	      </select>
	    </td>
      </tr>
	  <tr>
		<td colspan="2" align="center">
		<input type="submit" name="submit" value="<?=$Submit?>" class="Button">&nbsp;&nbsp;&nbsp;
		<input type="reset" name="reset" value="Làm lại" class="Button">&nbsp;&nbsp;&nbsp;
		<input type="button" name="back" value="Quay về" class="Button" onclick="GoBack('?mod=adv')"></td>
	  </tr>
	</table>
</form>
	